/**
 *
 */
package net.zantetu.matomeroid.activity;

import net.zantetu.matomeroid.R;
import net.zantetu.matomeroid.dao.PageInfoDao;
import net.zantetu.matomeroid.dto.PageHistoryDto;
import net.zantetu.matomeroid.dto.PageInfoDto;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

/**
 * idを渡すとそのページを開くActivity
 * @author aono_masashi
 *
 */
public class PageViewActivity extends Activity {

	private PageHistoryDto history;
	private final int MENU_FAVORITE = Menu.FIRST;
	private final int MENU_SEARCH = Menu.FIRST + 1;
	private final int MENU_HISTORY = Menu.FIRST + 2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_view);

		history = new PageHistoryDto();

		TextView tv = (TextView) findViewById(R.id.page_view_title_text);
		WebView wv = (WebView) findViewById(R.id.page_view_content_wv);
		PageViewClient client = new PageViewClient(this,tv,history);
		wv.setWebViewClient(client);
		wv.setHorizontalScrollBarEnabled(false);
		wv.loadUrl("file:///android_asset/index.html");

		// TODO favorite関連
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuItem favorite = menu.add(Menu.NONE,MENU_FAVORITE , Menu.NONE, "お気に入り");
		favorite.setIcon(android.R.drawable.ic_menu_add);
		MenuItem search = menu.add(Menu.NONE, MENU_SEARCH, Menu.NONE, "検索");
		search.setIcon(android.R.drawable.ic_menu_search);
		MenuItem history = menu.add(Menu.NONE, MENU_HISTORY, Menu.NONE, "履歴");
		history.setIcon(android.R.drawable.ic_menu_agenda);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_FAVORITE:
			// お気に入りの一覧を表示

			break;
		case MENU_SEARCH:
			// 検索窓を表示させる

			break;
		case MENU_HISTORY:
			// 履歴ページへ移動

			break;
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	/**
	 * webview client
	 * @author aono_masashi
	 *
	 */
	public class PageViewClient extends WebViewClient{

		private PageHistoryDto history;
		private TextView tv;
		private PageInfoDao dao;

		public PageViewClient(Context context, TextView tv, PageHistoryDto history) {
			this.tv = tv;
			this.dao = new PageInfoDao(context);
			this.history = history;
		}

		/* (非 Javadoc)
		 * @see android.webkit.WebViewClient#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap)
		 */
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// 履歴に追加
			PageInfoDto dto = dao.getPageInfoByUrl(url);
			history.addHistoryList(dto);

			super.onPageStarted(view, url, favicon);
		}

		/* (非 Javadoc)
		 * @see android.webkit.WebViewClient#onPageFinished(android.webkit.WebView, java.lang.String)
		 */
		@Override
		public void onPageFinished(WebView view, String url) {
			tv.setText(view.getTitle());
			super.onPageFinished(view, url);
		}
	}
}
